home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / gd25s.zip / CONTEXT.C < prev    next >
C/C++ Source or Header  |  1993-10-08  |  14KB  |  465 lines

  1. /* Context-format output routines for GNU DIFF.
  2.    Copyright (C) 1988, 89, 91, 92, 93 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "diff.h"
  21.  
  22. static struct change *find_hunk PARAMS((struct change *));
  23. static void find_function PARAMS((struct file_data const *, int, char const HUGE **, size_t *));
  24. static void mark_ignorable PARAMS((struct change *));
  25. static void pr_context_hunk PARAMS((struct change *));
  26. static void pr_unidiff_hunk PARAMS((struct change *));
  27. static void print_context_label PARAMS ((char const *, struct file_data *, char const *));
  28. static void print_context_number_range PARAMS((struct file_data const *, int, int));
  29. static void print_unidiff_number_range PARAMS((struct file_data const *, int, int));
  30.  
  31. /* Last place find_function started searching from.  */
  32. static int find_function_last_search;
  33.  
  34. /* The value find_function returned when it started searching there.  */
  35. static int find_function_last_match;
  36.  
  37. /* Print a label for a context diff, with a file name and date or a label.  */
  38.  
  39. static void
  40. print_context_label (mark, inf, label)
  41.      char const *mark;
  42.      struct file_data *inf;
  43.      char const *label;
  44. {
  45.   if (label)
  46.     fprintf (outfile, "%s %s\n", mark, label);
  47.   else
  48.     /* See Posix.2 section 4.17.6.1.4 for this format.  */
  49.     fprintf (outfile, "%s %s\t%s",
  50.          mark, inf->name, ctime (&inf->stat.st_mtime));
  51. }
  52.  
  53. /* Print a header for a context diff, with the file names and dates.  */
  54.  
  55. void
  56. print_context_header (inf, unidiff_flag)
  57.      struct file_data inf[];
  58.      int unidiff_flag;
  59. {
  60.   if (unidiff_flag)
  61.     {
  62.       print_context_label ("---", &inf[0], file_label[0]);
  63.       print_context_label ("+++", &inf[1], file_label[1]);
  64.     }
  65.   else
  66.     {
  67.       print_context_label ("***", &inf[0], file_label[0]);
  68.       print_context_label ("---", &inf[1], file_label[1]);
  69.     }
  70. }
  71.  
  72. /* Print an edit script in context format.  */
  73.  
  74. void
  75. print_context_script (script, unidiff_flag)
  76.      struct change *script;
  77.      int unidiff_flag;
  78. {
  79.   if (ignore_blank_lines_flag || ignore_regexp_list)
  80.     mark_ignorable (script);
  81.   else
  82.     {
  83.       struct change *e;
  84.       for (e = script; e; e = e->link)
  85.     e->ignore = 0;
  86.     }
  87.  
  88.   find_function_last_search = - files[0].prefix_lines;
  89.   find_function_last_match = find_function_last_search - 1;
  90.  
  91.   if (unidiff_flag)
  92.     print_script (script, find_hunk, pr_unidiff_hunk);
  93.   else
  94.     print_script (script, find_hunk, pr_context_hunk);
  95. }
  96.  
  97. /* Print a pair of line numbers with a comma, translated for file FILE.
  98.    If the second number is not greater, use the first in place of it.
  99.  
  100.    Args A and B are internal line numbers.
  101.    We print the translated (real) line numbers.  */
  102.  
  103. static void
  104. print_context_number_range (file, a, b)
  105.      struct file_data const *file;
  106.      int a, b;
  107. {
  108.   int trans_a, trans_b;
  109.   translate_range (file, a, b, &trans_a, &trans_b);
  110.  
  111.   /* Note: we can have B < A in the case of a range of no lines.
  112.      In this case, we should print the line number before the range,
  113.      which is B.  */
  114.   if (trans_b > trans_a)
  115.     fprintf (outfile, "%d,%d", trans_a, trans_b);
  116.   else
  117.     fprintf (outfile, "%d", trans_b);
  118. }
  119.  
  120. /* Print a portion of an edit script in context format.
  121.    HUNK is the beginning of the portion to be printed.
  122.    The end is marked by a `link' that has been nulled out.
  123.  
  124.    Prints out lines from both files, and precedes each
  125.    line with the appropriate flag-character.  */
  126.  
  127. static void
  128. pr_context_hunk (hunk)
  129.      struct change *hunk;
  130. {
  131.   int first0, last0, first1, last1, show_from, show_to, i;
  132.   struct change *next;
  133.   char const *prefix;
  134.   char const HUGE *function;
  135.   size_t function_length;
  136.   FILE *out;
  137.  
  138.   /* Determine range of line numbers involved in each file.  */
  139.  
  140.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
  141.  
  142.   if (!show_from && !show_to)
  143.     return;
  144.  
  145.   /* Include a context's width before and after.  */
  146.  
  147.   i = - files[0].prefix_lines;
  148.   first0 = max (first0 - context, i);
  149.   first1 = max (first1 - context, i);
  150.   last0 = min (last0 + context, files[0].valid_lines - 1);
  151.   last1 = min (last1 + context, files[1].valid_lines - 1);
  152.  
  153.   /* If desired, find the preceding function definition line in file 0.  */
  154.   function = 0;
  155.   if (function_regexp_list)
  156.     find_function (&files[0], first0, &function, &function_length);
  157.  
  158.   begin_output ();
  159.   out = outfile;
  160.  
  161.   /* If we looked for and found a function this is part of,
  162.      include its name in the header of the diff section.  */
  163.   fprintf (out, "***************");
  164.  
  165.   if (function)
  166.     {
  167.       fprintf (out, " ");
  168.       fwrite (function, 1, min (function_length - 1, 40), out);
  169.     }
  170.  
  171.   fprintf (out, "\n*** ");
  172.   print_context_number_range (&files[0], first0, last0);
  173.   fprintf (out, " ****\n");
  174.  
  175.   if (show_from)
  176.     {
  177.       next = hunk;
  178.  
  179.       for (i = first0; i <= last0; i++)
  180.     {
  181.       /* Skip past changes that apply (in file 0)
  182.          only to lines before line I.  */
  183.  
  184.       while (next && next->line0 + next->deleted <= i)
  185.         next = next->link;
  186.  
  187.       /* Compute the marking for line I.  */
  188.  
  189.       prefix = " ";
  190.       if (next && next->line0 <= i)
  191.         /* The change NEXT covers this line.
  192.            If lines were inserted here in file 1, this is "changed".
  193.            Otherwise it is "deleted".  */
  194.         prefix = (next->inserted > 0 ? "!" : "-");
  195.  
  196.       print_1_line (prefix, &files[0].linbuf[i]);
  197.     }
  198.     }
  199.  
  200.   fprintf (out, "--- ");
  201.   print_context_number_range (&files[1], first1, last1);
  202.   fprintf (out, " ----\n");
  203.  
  204.   if (show_to)
  205.     {
  206.       next = hunk;
  207.  
  208.       for (i = first1; i <= last1; i++)
  209.     {
  210.       /* Skip past changes that apply (in file 1)
  211.          only to lines before line I.  */
  212.  
  213.       while (next && next->line1 + next->inserted <= i)
  214.         next = next->link;
  215.  
  216.       /* Compute the marking for line I.  */
  217.  
  218.       prefix = " ";
  219.       if (next && next->line1 <= i)
  220.         /* The change NEXT covers this line.
  221.            If lines were deleted here in file 0, this is "changed".
  222.            Otherwise it is "inserted".  */
  223.         prefix = (next->deleted > 0 ? "!" : "+");
  224.  
  225.       print_1_line (prefix, &files[1].linbuf[i]);
  226.     }
  227.     }
  228. }
  229.  
  230. /* Print a pair of line numbers with a comma, translated for file FILE.
  231.    If the second number is smaller, use the first in place of it.
  232.    If the numbers are equal, print just one number.
  233.  
  234.    Args A and B are internal line numbers.
  235.    We print the translated (real) line numbers.  */
  236.  
  237. static void
  238. print_unidiff_number_range (file, a, b)
  239.      struct file_data const *file;
  240.      int a, b;
  241. {
  242.   int trans_a, trans_b;
  243.   translate_range (file, a, b, &trans_a, &trans_b);
  244.  
  245.   /* Note: we can have B < A in the case of a range of no lines.
  246.      In this case, we should print the line number before the range,
  247.      which is B.  */
  248.   if (trans_b <= trans_a)
  249.     fprintf (outfile, trans_b == trans_a ? "%d" : "%d,0", trans_b);
  250.   else
  251.     fprintf (outfile, "%d,%d", trans_a, trans_b - trans_a + 1);
  252. }
  253.  
  254. /* Print a portion of an edit script in unidiff format.
  255.    HUNK is the beginning of the portion to be printed.
  256.    The end is marked by a `link' that has been nulled out.
  257.  
  258.    Prints out lines from both files, and precedes each
  259.    line with the appropriate flag-character.  */
  260.  
  261. static void
  262. pr_unidiff_hunk (hunk)
  263.      struct change *hunk;
  264. {
  265.   int first0, last0, first1, last1, show_from, show_to, i, j, k;
  266.   struct change *next;
  267.   char const HUGE *function;
  268.   size_t function_length;
  269.   FILE *out;
  270.  
  271.   /* Determine range of line numbers involved in each file.  */
  272.  
  273.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
  274.  
  275.   if (!show_from && !show_to)
  276.     return;
  277.  
  278.   /* Include a context's width before and after.  */
  279.  
  280.   i = - files[0].prefix_lines;
  281.   first0 = max (first0 - context, i);
  282.   first1 = max (first1 - context, i);
  283.   last0 = min (last0 + context, files[0].valid_lines - 1);
  284.   last1 = min (last1 + context, files[1].valid_lines - 1);
  285.  
  286.   /* If desired, find the preceding function definition line in file 0.  */
  287.   function = 0;
  288.   if (function_regexp_list)
  289.     find_function (&files[0], first0, &function, &function_length);
  290.  
  291.   begin_output ();
  292.   out = outfile;
  293.  
  294.   fprintf (out, "@@ -");
  295.   print_unidiff_number_range (&files[0], first0, last0);
  296.   fprintf (out, " +");
  297.   print_unidiff_number_range (&files[1], first1, last1);
  298.   fprintf (out, " @@");
  299.  
  300.   /* If we looked for and found a function this is part of,
  301.      include its name in the header of the diff section.  */
  302.  
  303.   if (function)
  304.     {
  305.       putc (' ', out);
  306.       fwrite (function, 1, min (function_length - 1, 40), out);
  307.     }
  308.   putc ('\n', out);
  309.  
  310.   next = hunk;
  311.   i = first0;
  312.   j = first1;
  313.  
  314.   while (i <= last0 || j <= last1)
  315.     {
  316.  
  317.       /* If the line isn't a difference, output the context from file 0. */
  318.  
  319.       if (!next || i < next->line0)
  320.     {
  321.       putc (tab_align_flag ? '\t' : ' ', out);
  322.       print_1_line (0, &files[0].linbuf[i++]);
  323.       j++;
  324.     }
  325.       else
  326.     {
  327.       /* For each difference, first output the deleted part. */
  328.  
  329.       k = next->deleted;
  330.       while (k--)
  331.         {
  332.           putc ('-', out);
  333.           if (tab_align_flag)
  334.         putc ('\t', out);
  335.           print_1_line (0, &files[0].linbuf[i++]);
  336.         }
  337.  
  338.       /* Then output the inserted part. */
  339.  
  340.       k = next->inserted;
  341.       while (k--)
  342.         {
  343.           putc ('+', out);
  344.           if (tab_align_flag)
  345.         putc ('\t', out);
  346.           print_1_line (0, &files[1].linbuf[j++]);
  347.         }
  348.  
  349.       /* We're done with this hunk, so on to the next! */
  350.  
  351.       next = next->link;
  352.     }
  353.     }
  354. }
  355.  
  356. /* Scan a (forward-ordered) edit script for the first place that more than
  357.    2*CONTEXT unchanged lines appear, and return a pointer
  358.    to the `struct change' for the last change before those lines.  */
  359.  
  360. static struct change *
  361. find_hunk (start)
  362.      struct change *start;
  363. {
  364.   struct change *prev;
  365.   int top0, top1;
  366.   int thresh;
  367.  
  368.   do
  369.     {
  370.       /* Compute number of first line in each file beyond this changed.  */
  371.       top0 = start->line0 + start->deleted;
  372.       top1 = start->line1 + start->inserted;
  373.       prev = start;
  374.       start = start->link;
  375.       /* Threshold distance is 2*CONTEXT between two non-ignorable changes,
  376.      but only CONTEXT if one is ignorable.  */
  377.       thresh = ((prev->ignore || (start && start->ignore))
  378.         ? context
  379.         : 2 * context + 1);
  380.       /* It is not supposed to matter which file we check in the end-test.
  381.      If it would matter, crash.  */
  382.       if (start && start->line0 - top0 != start->line1 - top1)
  383.     abort ();
  384.     } while (start
  385.          /* Keep going if less than THRESH lines
  386.         elapse before the affected line.  */
  387.          && start->line0 < top0 + thresh);
  388.  
  389.   return prev;
  390. }
  391.  
  392. /* Set the `ignore' flag properly in each change in SCRIPT.
  393.    It should be 1 if all the lines inserted or deleted in that change
  394.    are ignorable lines.  */
  395.  
  396. static void
  397. mark_ignorable (script)
  398.      struct change *script;
  399. {
  400.   while (script)
  401.     {
  402.       struct change *next = script->link;
  403.       int first0, last0, first1, last1, deletes, inserts;
  404.  
  405.       /* Turn this change into a hunk: detach it from the others.  */
  406.       script->link = 0;
  407.  
  408.       /* Determine whether this change is ignorable.  */
  409.       analyze_hunk (script, &first0, &last0, &first1, &last1, &deletes, &inserts);
  410.       /* Reconnect the chain as before.  */
  411.       script->link = next;
  412.  
  413.       /* If the change is ignorable, mark it.  */
  414.       script->ignore = (!deletes && !inserts);
  415.  
  416.       /* Advance to the following change.  */
  417.       script = next;
  418.     }
  419. }
  420.  
  421. /* Find the last function-header line in FILE prior to line number LINENUM.
  422.    This is a line containing a match for the regexp in `function_regexp'.
  423.    Store the address of the line text into LINEP and the length of the
  424.    line into LENP.
  425.    Do not store anything if no function-header is found.  */
  426.  
  427. static void
  428. find_function (file, linenum, linep, lenp)
  429.      struct file_data const *file;
  430.      int linenum;
  431.      char const HUGE **linep;
  432.      size_t *lenp;
  433. {
  434.   int i = linenum;
  435.   int last = find_function_last_search;
  436.   find_function_last_search = i;
  437.  
  438.   while (--i >= last)
  439.     {
  440.       /* See if this line is what we want.  */
  441.       struct regexp_list *r;
  442.       char const HUGE *line = file->linbuf[i];
  443.       size_t len = file->linbuf[i + 1] - line;
  444.  
  445.       for (r = function_regexp_list; r; r = r->next)
  446.     if (0 <= re_search (&r->buf, line, len, 0, len, 0))
  447.       {
  448.         *linep = line;
  449.         *lenp = len;
  450.         find_function_last_match = i;
  451.         return;
  452.       }
  453.     }
  454.   /* If we search back to where we started searching the previous time,
  455.      find the line we found last time.  */
  456.   if (find_function_last_match >= - file->prefix_lines)
  457.     {
  458.       i = find_function_last_match;
  459.       *linep = file->linbuf[i];
  460.       *lenp = file->linbuf[i + 1] - *linep;
  461.       return;
  462.     }
  463.   return;
  464. }
  465.